package com.xinjun.kingstar.ui.activites;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.view.MenuItem;
import android.widget.FrameLayout;

import com.hengjun.kingstar.sdk.base.activity.BaseCompatActivity;
import com.hengjun.kingstar.sdk.helper.BottomNavigationViewHelper;
import com.hengjun.kingstar.sdk.rxbus.RxBus;
import com.xinjun.kingstar.R;
import com.xinjun.kingstar.ui.fragments.home.HomeRootFragment;
import com.xinjun.kingstar.ui.fragments.setting.SettingRootFragment;
import com.xinjun.kingstar.ui.fragments.vip.VipRootFragment;

import butterknife.BindView;
import me.yokeyword.fragmentation.SupportFragment;

/**
 * 主页面
 */
public class MainActivity extends BaseCompatActivity {

    @BindView(R.id.fl_container)
    FrameLayout flContainer;
    @BindView(R.id.bottom_nv)
    BottomNavigationView bottomNv;

    //分别标识三个底部tab所在的fragment
    public static final int FIRST = 0;
    public static final int SECOND = 1;
    public static final int THIRD = 2;

    private SupportFragment[] mFragments = new SupportFragment[3];//装根fragment的集合

    /**
     * 初始化布局
     *
     * @param savedInstanceState savedInstanceState
     */
    @Override
    protected void initView(Bundle savedInstanceState) {
        if (savedInstanceState == null) {
            //实例化每个fragment
            mFragments[FIRST] = HomeRootFragment.newInstance();
            mFragments[SECOND] = VipRootFragment.newInstance();
            mFragments[THIRD] = SettingRootFragment.newInstance();
            //加载fragment数据到帧布局
            loadMultipleRootFragment(R.id.fl_container, FIRST,
                    mFragments[FIRST],
                    mFragments[SECOND],
                    mFragments[THIRD]);
        } else {
            // 这里库已经做了Fragment恢复,所有不需要额外的处理了, 不会出现重叠问题
            // 这里我们需要拿到mFragments的引用,也可以通过getSupportFragmentManager.getFragments()
            // 自行进行判断查找(效率更高些),用下面的方法查找更方便些
            mFragments[FIRST] = findFragment(HomeRootFragment.class);
            mFragments[SECOND] = findFragment(VipRootFragment.class);
            mFragments[THIRD] = findFragment(SettingRootFragment.class);
        }

        BottomNavigationViewHelper.disableShiftMode(bottomNv);//BottomNavigationView禁止3个item以上动画切换效果

        //bottomNv监听
        bottomNv.setOnNavigationItemSelectedListener(new BottomNavigationView
                .OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {//导航栏监听事件
                switch (item.getItemId()) {
                    case R.id.action_home://首页
                        showHideFragment(mFragments[FIRST]);
                        break;
                    case R.id.action_vip://vip
                        showHideFragment(mFragments[SECOND]);
                        break;
                    case R.id.action_setting://设置
                        showHideFragment(mFragments[THIRD]);
                        break;
                }
                return true;
            }
        });
    }

    /**
     * 写布局
     *
     * @return id
     */
    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    /**
     * 初始化数据
     */
    @Override
    protected void initData() {
        super.initData();
        //Logger.e("RxBus.get().register(this)");
        RxBus.get().register(this);//注册rxBUs
        bottomNv.setItemIconTintList(null);//不设置选中后图片的颜色
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //Logger.e("RxBus.get().unRegister(this)");
        RxBus.get().unRegister(this);//解绑rxBus
    }


}
